package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.HistoryExData;
import com.tbit.main.constant.SysConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 摆车
 *
 * @author LMY
 * @create 2021-01-08 15:43
 */
@RestController
@RequestMapping("/neat")
public class MachineNeatLogController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private MachineNeatService machineNeatService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private GeoreMachineService georeMachineService;

    /**
     * 添加
     *
     * @param token
     * @param machineNeatLog
     * @param lon
     * @param lat
     * @param mapType
     * @return
     */
    @RequestMapping("/insert")
    public JsonResult insert(String token, MachineNeatLog machineNeatLog, Double lon, Double lat, Integer mapType) {
        AccountUser accountUserToken = tokenService.getAccount(token);
        Assert.hasLength(machineNeatLog.getUserCode(), "Machine.userCode.notNull");
        Assert.hasLength(machineNeatLog.getBeforeImageIds(), "MachineNeat.beforeImages.notNull");
        Assert.hasLength(machineNeatLog.getAfterImageIds(), "MachineNeat.afterImages.notNull");

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        /**查询设备信息*/
        Machine machine = machineService.getByUseCode(machineNeatLog.getUserCode());
        if (machine == null) {
            throw new BaseException("Machine.notExist");
        }

        //workOrderService.startWork(WorkOrderConstant.workType_BC,accountUserToken,machine);

        /**设备权限判断*/
        machineService.auth(accountUserToken.getAgents(), machine.getMachineId());

        machineNeatLog.setMachineId(machine.getMachineId());
        machineNeatLog.setMachineNO(machine.getMachineNO());
        machineNeatLog.setAccountId(machine.getAccountId());
        machineNeatLog.setAccountUserId(accountUserToken.getAccountUserId());
        machineNeatLog.setUserName(accountUserToken.getName());
        machineNeatLog.setIsValid(1);
        if (machineNeatLog.getRemark() == null) {
            machineNeatLog.setRemark("");
        }

        /**
         * 判断分区
         */
        GeoreMachine georeMachine = georeMachineService.getByMachineId(machine.getMachineId());
        if (georeMachine != null) {
            machineNeatLog.setRegionId(georeMachine.getRegionId());
        }

        machineNeatService.insert(machineNeatLog);

        //workOrderService.endWork(WorkOrderConstant.workType_BC,accountUserToken,machine.getMachineId(),machineNeatLog.getMachineNeatLogId()+"",1);

        /**记录运维事件轨迹*/
        try {
            TerPosition terPosition = new TerPosition();
            terPosition.setMachineNO(accountUserToken.getPhone());
            terPosition.setLat(lat);
            terPosition.setLon(lon);
            terPosition.setDt(DateTimeUtil.getNowTime());
            terPosition.setExData(HistoryExData.UW + "=" + HistoryExData.BC + ";");
            calibrateService.calTerPosition(terPosition, mapType, SysConstant.ORIGINAL);
            historyService.insert(terPosition);
        } catch (Exception e) {
            LogUtil.info(e.toString());
        }

        return JsonResult.succ();
    }

    /**
     * 分组统计
     */
    @RequestMapping("/groupByTime")
    public JsonResult groupByTime(String token, Integer accountId, String startTime, String endTime, Integer isValid,
                                  @RequestParam(value = "regionId", required = false) List<Integer> regionId) {
        AccountUser accountUserToken = tokenService.getAccount(token);
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<MachineNeatLogSta> machineNeatLogs = machineNeatService.groupByTime(accountId, accountUserToken.getAccountUserType(), accountUserToken.getAccountUserId(), startTime, endTime, isValid, regionId);

        return JsonResult.succ(machineNeatLogs);
    }

    /**
     * 根据时间查询
     */
    @RequestMapping("/getByTime")
    public JsonResult getByTime(String token, Integer accountId, Integer userMainId, String startTime, String endTime, Integer isValid,
                                @RequestParam(value = "regionId", required = false) List<Integer> regionId) {
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");


        List<MachineNeatLog> machineNeatLogs = machineNeatService.getByTime(accountId, userMainId, startTime, endTime, isValid, regionId);

        return JsonResult.succ(machineNeatLogs);
    }
}
